clear all

set maxvar 100000
set more off

run "doFiles/bayesianPosteriors.do"

clear

import delimited "data/raw/amountsSpotlightMturk.csv" , case(preserve) 
cap rename ïwin win
save "data/amounts.dta", replace
clear


import delimited "data/raw/amountsRiskSpotlightMturk.csv" , case(preserve) 
cap rename ïround round
sort round
rename win winRisk
rename lose loseRisk
rename round roundRisk
save "data/amountsRiskSpotlightMturk.dta", replace


clear

import delimited "data/raw/main1.tsv" , case(preserve) delimiter(tab) varnames(1) bindquote(strict)
destring, replace
keep if prior == "mid"	// For other priors, states in the experiment were mistakenly drawn with a 50% prior, too.
save "data/main1.dta", replace

clear

import delimited "data/raw/main2.tsv" , case(preserve) delimiter(tab) varnames(1) bindquote(strict)
destring, replace
save "data/main2.dta", replace

append using "data/main1.dta"






// Study attrition 
if 1 {
	count
	// 845 subjects who started the survey
	
	count if techCheck ~= . 
	// 614 subjects who could technically display the survey
	
	
	tab Progress if compCheck1_DO_1 ~= .
	tab Progress
	// Completion of comprehension check is saved as Progress >= 35
	
	count if Progress <= 33 & techCheck ~= . 
	// 70 drop out during the instructions before reaching the comprehension check
	
	count if Progress > 33 & techCheck ~= . 
	// 544 progress to comprehension check
	
	count if compCheck1_DO_1 ~= . 
	// 440 pass the first comprehension check (i.e. 104 drop out)
	
	count if compCheck2_DO_4 ~= . 
	// 429 subjects passed the second comprehension check
	
	count if male ~= . 
	// 405 subjects who completed the entire survey
	
	// Accordingly 24 more individuals gave up after the second comprehension check
	
	gen droppedOut = techCheck ~= . & male == .
	
	tab prior delay if droppedOut == 1, chi2
	
	gen priorDelay = priorNum * delay
	reg droppedOut priorNum delay priorDelay
	drop priorDelay
	
	
}




keep if male ~= . 
drop compCheck*

forvalues r = 1 / 16 {
	rename r`r'_bet 				bet`r' 			
	rename r`r'_belief 				belief`r' 			
	rename r`r'_beliefReverse		beliefReverse`r'	

	di `r'

	replace belief`r' = beliefReverse`r' if belief`r' == . 
}


rename beliefReverse reverseBeliefElicitation

drop beliefReverse*

gen id = _n


drop state





// Data management risk decisions
if 1 {
	preserve
	
		forvalues r = 1 / 28 {
			
			split ceScramble`r', parse(",", "[", "]") gen(r`r'_ceValue)
			destring r`r'_ceValue*, force replace
			
			drop r`r'_ceValue1
			forvalues i = 2 / 12 {
				local iMinus = `i' - 1
				rename r`r'_ceValue`i' r`r'_ceValue`iMinus'
			}
		}
		

		forvalues r = 1 / 28 {
			forvalues i = 1 / 11 {
				rename r`r'_risk`i' risk`i'_`r'
				rename r`r'_ceValue`i' ceValue`i'_`r'
			}
		}

		keep id risk* delayTreatment prior ceValue*
		
		reshape long risk1_ risk2_ risk3_ risk4_ risk5_ risk6_ risk7_ risk8_ risk9_ risk10_ risk11_ ///
					 ceValue1_ ceValue2_ ceValue3_ ceValue4_ ceValue5_ ceValue6_ ceValue7_ ceValue8_ ceValue9_ ceValue10_ ceValue11_, i(id) j(round)

		
		forvalues i = 1 / 11 {
			rename risk`i'_ risk`i'
			rename ceValue`i'_ ceValue`i'
		}
		
		reshape long risk ceValue, i(id round) j(decision)
				
		sort id round ceValue
		
		
		rename risk riskChoice
		// The variable riskChoice equals 1 if the lottery is chosen and 2 if the certainty equivalent is chosen
		
 		
		gen switchBack_i = riskChoice[_n] - riskChoice[_n-1] == -1
		replace switchBack_i = . if ceValue == -6
		bysort id round: egen switchBack = max(switchBack_i)
		
		bysort id: egen fracMultiSwitch = mean(switchBack)
		bysort id: egen everMultiSwitch = max(switchBack)
			
		replace riskChoice = riskChoice - 1
		// Now riskChoice equals 0 if the lottery is chosen and 1 if the certainty equivalent is chosen
		replace riskChoice = . if riskChoice == 0
		replace riskChoice = riskChoice * ceValue
		
		bysort id round: egen minCE = min(riskChoice)
		// minCE is the lowest certainty equivalent that is better than the lottery. 
		// Hence, it's an upper bound on the certainty equivalent. 
		
		// For subjects who select the lottery all the way through, the certainty equivalent must exceed 12. 
		// These subjects' data will show as missing. Replace it with 15. 
		replace minCE = 6 if minCE == .

		egen tagRisk = tag(id round) 
		
		keep if tagRisk == 1
		drop tagRisk
		drop decision riskChoice switchBack_i ceValue
		
		
		rename round roundRisk
		merge m:1 roundRisk using "data/amountsRiskSpotlightMturk.dta"
		drop _m
		sort id round
		
		rename roundRisk round
	
		
		cap rename Catastrophe catastropheTreatment
		
		gen lose = abs(win - 6)
		replace lose = 6 if catastropheTreatment == 1
		gen ev = bayesProbExo * win - (1 - bayesProbExo) * lose
		
			
		cap gen riskAversion_i = ev - minCE if bayesProbExo ~= .
		bysort id: egen riskAversion = mean(riskAversion_i)
		cap drop riskAversion_i
			
				
				
				
		// Certainty equivalent: use midpoint
		if 1 {	
				
			gen minCEmid = .
			replace minCEmid = -12 		/2 if round(minCE * 100) ==  round(100*(-12	/ 2))
			replace minCEmid = -10.5 	/2 if round(minCE * 100) ==  round(100*(-9	/ 2))
			replace minCEmid = -7.5  	/2 if round(minCE * 100) ==  round(100*(-6	/ 2))
			replace minCEmid = -4.5  	/2 if round(minCE * 100) ==  round(100*(-3	/ 2))
			replace minCEmid = -2  		/2 if round(minCE * 100) ==  round(100*(-1	/ 2))
			replace minCEmid = -0.5 	/2 if round(minCE * 100) ==  round(100*(0 	/ 2))
			replace minCEmid = 0.5 		/2 if round(minCE * 100) ==  round(100*(1 	/ 2))
			replace minCEmid = 2 		/2 if round(minCE * 100) ==  round(100*(3 	/ 2))
			replace minCEmid = 4.5 		/2 if round(minCE * 100) ==  round(100*(6 	/ 2))
			replace minCEmid = 7.5 		/2 if round(minCE * 100) ==  round(100*(9 	/ 2))
			replace minCEmid = 10.5 	/2 if round(minCE * 100) ==  round(100*(12	/ 2))
			replace minCEmid = 12	 	/2 if round(minCE * 100) ==  round(100*(15	/ 2))

			replace minCE = minCEmid
			drop minCEmid
			
		}
		
				
		bysort id: egen exoCElow_i = mean(minCE) if bayesProbExo == 20
		bysort id: egen exoCEmid_i = mean(minCE) if bayesProbExo == 50
		bysort id: egen exoCEhigh_i = mean(minCE) if bayesProbExo == 80

		bysort id: egen exoCElow  = mean(exoCElow_i )
		bysort id: egen exoCEmid  = mean(exoCEmid_i )
		bysort id: egen exoCEhigh = mean(exoCEhigh_i)
		
		save "data/riskSwitchPointsExp3.dta", replace
	restore
}


drop *risk*





// RESHAPING HERE


drop signal17-signal40
drop letterSeen17-letterSeen20
drop gridSeen17-gridSeen20 
drop gridSeenAll17-gridSeenAll20 
drop locationsX17-locationsX20
drop locationsY17-locationsY20
drop timeUnique17-timeUnique20
drop timeUniqueAll17-timeUniqueAll20
drop letterSeenUnique17-letterSeenUnique20
drop letterSeenUniqueAll17-letterSeenUniqueAll20
drop timeAll17-timeAll20

drop letterSeen gridSeen locationsX locationsY timeUnique letterSeenUnique  timeAll

cap tostring timeUnique12, replace

reshape long infoStr bet betAfter belief signal order changeDecision clickCont state letterSeen letterSeenUnique letterSeenUniqueAll gridSeen gridSeenAll locationsX locationsY timeUnique timeUniqueAll timeAll bayesProb, i(id) j(round) 

drop if round > 16

replace clickCont = 0 if clickCont == .


// drop signal 
// rename signalNum signal

merge m:1 round using "data/amounts.dta"
drop _m
merge m:n id round using "data/riskSwitchPointsExp3.dta"







sort id round

gen highInc = win >= 3
gen lowInc = win < 3

replace age = age + 18
egen tag = tag(id)










gen treatment = .
replace treatment = 1 if win >  4 & known == 1 
replace treatment = 2 if win == 4 & known == 1 
replace treatment = 3 if win == 1 & known == 1 
replace treatment = 4 if win <  1 & known == 1 
replace treatment = 5 if win >  4 & known == 0 
replace treatment = 6 if win == 4 & known == 0 
replace treatment = 7 if win == 1 & known == 0 
replace treatment = 8 if win <  1 & known == 0 


// drop individuals for whom there was an error displaying the letters (variable "signal" is empty). 
// drop if id == 1 | id == 92


cap  gen weight = .
su treatment
local numTreatments = r(max)
forvalues t = 1 / `numTreatments' {
	su state if treatment == `t' 
	replace weight = 1 / r(mean)			if state == 1 & treatment == `t'
	replace weight = 1 / (1 - r(mean)) 		if state == 0 & treatment == `t'
}






replace bet = betAfter if beliefFirst == 1

keep if probCourse ~= . 



// Define variables 
if 1 {
	
	drop if round == .
	replace bet = 0 if bet == 2 
		
	bysort id: egen everBetLow_i = max(bet) if highInc == 0
	bysort id: egen everAbstainHigh_i = min(bet) if highInc == 1
	bysort id: egen everBetLow = mean(everBetLow_i)
	bysort id: egen everAbstainHigh = mean(everAbstainHigh_i)
	gen priorOnly = everBetLow == 0 & everAbstainHigh == 0

	cap gen highInc_known = highInc * known
	cap gen beliefDev = belief - bayesPost
	cap gen absBeliefDev = abs(beliefDev)
	
	gen lowIncKnown 		= highInc == 0 & known == 1
	gen highIncKnown 		= highInc == 1 & known == 1
	gen lowIncUnknown 		= highInc == 0 & known == 0
	gen highIncUnknown 		= highInc == 1 & known == 0
	
	cap drop date
	cap rename ÿþStartDate StartDate
	split StartDate, parse(" ")
	gen date = date(StartDate1, "MDY", 2100)
	gen day =  day(date)
		
		
	// Coding the signals received
	if 1 {
		// Only unique letters
		if 1 {
			egen numG = noccur(letterSeenUnique) , string("G")
			egen numB = noccur(letterSeenUnique) , string("B")
			gen signalLength = numG + numB
			gen signalDiff = numG - numB
			gen fracG = numG / signalLength
			
			gen strLength = strlen(letterSeenUnique)
			
			cap drop _m
			sort numG numB

			merge m:1 numG numB using "data/bayesPost20.dta"
			drop if _m == 2
			drop _m
			rename bayesPost bayesPost20 
			
			merge m:1 numG numB using "data/bayesPost50.dta"
			drop if _m == 2
			drop _m
			rename bayesPost bayesPost50 
			
			merge m:1 numG numB using "data/bayesPost80.dta"
			drop if _m == 2
			drop _m
			rename bayesPost bayesPost80
			
			gen bayesPost = . 
			replace bayesPost = bayesPost20 if prior == "low"
			replace bayesPost = bayesPost50 if prior == "mid"
			replace bayesPost = bayesPost80 if prior == "high"
		}					
	}
}


// Extract duration for which subjects examined the pictures
if 1 {
	preserve 
		keep id round timeAll 
		split timeAll, p(",")
		destring, replace
		drop timeAll
		egen maxTime = rowmax(timeAll*)
		keep id round maxTime
		replace maxTime = maxTime / 1000
		save "data/pictureDuration.dta", replace
	restore 
}


merge 1:1 id round using "data/pictureDuration.dta"



replace minCE = . if id <= 19

save "data/data.dta", replace


